package com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.utils.BookCityUtils;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedEightLayout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedFiveLayout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedFour1Layout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedFour2Layout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedFour3Layout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedFour4Layout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedFour5Layout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedFour6Layout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedOneLayout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedSevenLayout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedSixLayout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedThreeLayout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.AdSelectedTwoLayout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.DefaultItemLayout;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.IRefresh;

import java.util.ArrayList;
import java.util.List;

/**
 * 精选/男频/女频适配器
 * Created by wcy on 18/5/15.
 */
public class SelectedTopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int HEADER_VIEW = 0x00000111;
    public static final int FOOTER_VIEW = 0x00000112;
    private List<SelectedBean> mDatas;
    private Context mContext;
    // head and footer
    protected View mHeaderView;
    protected View mFooterView;
    // 精选 男频 女频
    private int mTabType;
    private List<IRefresh> mRefreshList = new ArrayList<>();

    public SelectedTopAdapter(Context context, List<SelectedBean> datas, int type) {
        mContext = context;
        mDatas = datas;
        mTabType = type;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder;
        switch (viewType) {
            case HEADER_VIEW:
                baseViewHolder = new BaseViewHolder(mHeaderView);
                break;
            case FOOTER_VIEW:
                baseViewHolder = new BaseViewHolder(mFooterView);
                break;
            case SelectedBean.TYPE_ONE:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdSelectedOneLayout(parent.getContext())));
                break;
            case SelectedBean.TYPE_TWO:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdSelectedTwoLayout(parent.getContext())));
                break;
            case SelectedBean.TYPE_THREE:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdSelectedThreeLayout(parent.getContext())));
                break;
            case SelectedBean.TYPE_FOUR_1:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdSelectedFour1Layout(parent.getContext())));
                break;
            case SelectedBean.TYPE_FOUR_2:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdSelectedFour2Layout(parent.getContext())));
                break;
            case SelectedBean.TYPE_FOUR_3:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdSelectedFour3Layout(parent.getContext())));
                break;
            case SelectedBean.TYPE_FOUR_4:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdSelectedFour4Layout(parent.getContext())));
                break;
            case SelectedBean.TYPE_FOUR_5:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdSelectedFour5Layout(parent.getContext())));
                break;
            case SelectedBean.TYPE_FOUR_6:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdSelectedFour6Layout(parent.getContext())));
                break;
            case SelectedBean.TYPE_FIVE:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdSelectedFiveLayout(parent.getContext())));
                break;
            case SelectedBean.TYPE_SIX:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdSelectedSixLayout(parent.getContext())));
                break;
            case SelectedBean.TYPE_SEVEN:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdSelectedSevenLayout(parent.getContext())));
                break;
            case SelectedBean.TYPE_EIGHT:
                baseViewHolder = new BaseViewHolder(wrapItemView(new AdSelectedEightLayout(parent.getContext())));
                break;
            default:
                baseViewHolder = new BaseViewHolder(wrapItemView(new DefaultItemLayout(parent.getContext())));
                break;
        }
        return baseViewHolder;
    }

    private ViewGroup wrapItemView(ViewGroup viewGroup) {
        viewGroup.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        if (viewGroup instanceof IRefresh) {
            mRefreshList.add((IRefresh) viewGroup);
        }
        return viewGroup;

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        BaseViewHolder baseViewHolder = (BaseViewHolder) holder;
        switch (getItemViewType(position)) {
            case HEADER_VIEW:
                break;
            case FOOTER_VIEW:
                break;
                // 8中模板
            case SelectedBean.TYPE_ONE:
            case SelectedBean.TYPE_TWO:
            case SelectedBean.TYPE_THREE:
            case SelectedBean.TYPE_FOUR_1:
            case SelectedBean.TYPE_FOUR_2:
            case SelectedBean.TYPE_FOUR_3:
            case SelectedBean.TYPE_FOUR_4:
            case SelectedBean.TYPE_FOUR_5:
            case SelectedBean.TYPE_FOUR_6:
            case SelectedBean.TYPE_FIVE:
            case SelectedBean.TYPE_SIX:
            case SelectedBean.TYPE_SEVEN:
            case SelectedBean.TYPE_EIGHT:
                View convertView = baseViewHolder.getConvertView();
                if (convertView instanceof IRefresh) {
                    ((IRefresh) convertView).refresh(getItem(position),
                            BookCityUtils.getItemRefreshBean(mTabType, position - getHeaderViewsCount(), mDatas, this));
                }
                break;
            default:
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size() + getHeaderViewsCount() + getFooterViewsCount();
    }
    public int getHeaderViewsCount() {
        return mHeaderView == null ? 0 : 1;
    }
    public int getFooterViewsCount() {
        return mFooterView == null ? 0 : 1;
    }
    public View getHeaderView() {
        return mHeaderView;
    }
    public void addHeaderView(View headerView) {
        headerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        mHeaderView = headerView;
        notifyItemChanged(0);
    }
    public View getFooterView() {
        return mFooterView;
    }
    public void addFooterView(View footerView) {
        footerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        mFooterView = footerView;
        notifyItemChanged(0);
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView != null && position == 0) {
            return HEADER_VIEW;
        }
        if (mFooterView != null && position == getItemCount() - 1) {
            return FOOTER_VIEW;
        }
        return BookCityUtils.getIntType(mDatas.get(position - getHeaderViewsCount()).type);
    }
    public SelectedBean getItem(int pos) {
        pos = pos - getHeaderViewsCount();
        return mDatas.get(pos);
    }

    public void setDatas(List<SelectedBean> list) {
        mDatas = list;
        notifyDataSetChanged();
    }

    public List<SelectedBean>  getDatas() {
        return mDatas;
    }

    public void reset() {
        for (IRefresh iRefresh : mRefreshList) {
            if (iRefresh != null) {
                iRefresh.reset();
            }
        }
    }

    public void clear() {
        mRefreshList.clear();
    }

}
